//
//  CircelLayout.m
//  UICollectionViewFlowLayoutTest
//
//  Created by Rocky on 16/4/14.
//  Copyright © 2016年 Rocky. All rights reserved.
//

#import "CircelLayout.h"
#define ITEM_SIZE 20
@interface CircelLayout ()
{
    NSMutableArray * arrDelectItems;
    NSMutableArray* arrAddItem;
}
@end
@implementation CircelLayout


-(void)prepareLayout
{
    [super prepareLayout];
    cellCount = [self.collectionView numberOfItemsInSection:0];
    CGRect rect = self.collectionView.frame;
    //找出中心点
    center = CGPointMake(rect.size.width/2, rect.size.height/2);
    radius = MIN(rect.size.width, rect.size.height)/2.5;
}

//返回 设置每个itme的属性值；
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    attributes.center = CGPointMake(center.x + radius * cosf(2 * indexPath.item * M_PI / cellCount),
                                    center.y + radius * sinf(2 * indexPath.item * M_PI / cellCount));
    return attributes;
    
}

//用来在一开始给出一套UICollectionViewLayoutAttributes
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray* attributes = [NSMutableArray array];
    NSInteger section = [self.collectionView numberOfSections];
    for (NSInteger j=0; j<section; j++)
    {
        cellCount = [self.collectionView numberOfItemsInSection:j];
        for (NSInteger i=0 ; i < cellCount; i++)
        {
            NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:j];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
    }
    return attributes;
}
//插入或册除 都会先更新这个方法
-(void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems
{
    [super prepareForCollectionViewUpdates:updateItems];
     NSLog(@"插入或册除 都会先更新这个方法===cellCount==%lu",cellCount);
    //
    cellCount = [self.collectionView numberOfItemsInSection:0];
    arrDelectItems = [NSMutableArray arrayWithCapacity:1];
    arrAddItem = [NSMutableArray array];
    for (UICollectionViewUpdateItem * item in updateItems)
    {
        if (item.updateAction ==UICollectionUpdateActionDelete)
        {
            [arrDelectItems addObject:item.indexPathBeforeUpdate];
        }else if (item.updateAction == UICollectionUpdateActionInsert)
        {
            [arrAddItem addObject:item.indexPathAfterUpdate];
        }
    }
   
}

//更新结束后调用
-(void)finalizeCollectionViewUpdates
{
   [super finalizeCollectionViewUpdates];
    arrDelectItems = arrAddItem = nil;
}
//即将显示item
- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    //
    UICollectionViewLayoutAttributes *attributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
    if ([arrAddItem containsObject:itemIndexPath])
    {
        if (!attributes)
        {
            attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
        }
        attributes.alpha = 0.0;
        attributes.center = CGPointMake(center.x, center.y);
    }
    return attributes;
}

//即将更新消失的item
-(UICollectionViewLayoutAttributes*)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes *attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
    if ([arrDelectItems containsObject:itemIndexPath])
    {
        if (!attributes)
        {
            attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:itemIndexPath];
        }
        attributes.alpha = 0.0;
        attributes.center = CGPointMake(center.x, center.y);
        attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
    }
    return attributes;
}


@end
